package com.fastloan.mgt.chatapp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.mgt.customer.enums.CustomerEnums;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.verify.enums.ApiStore;
import com.fastloan.mgt.verify.model.ApiResult;
import com.fastloan.mgt.verify.util.ApiUtil;
import com.fastloan.mgt.verify.util.GetParam;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.fastloan.utils.PropertiesUtil;
import com.kawins.result.ResultTo;
import com.kawins.spring.ContextProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import static com.fastloan.utils.UrlEncryptUtil.deCryptAndDecode;
import static com.fastloan.utils.UrlEncryptUtil.enCryptAndEncode;


/**
 * @author chenkai
 * createTime 2017-12-2017/12/11 15:48
 */
@Controller
@RequestMapping("/api/livingIdentity")
public class LivingIdentityController {
    @Autowired
    private ZyCustomerService customerService;

    @Autowired
    private CmuUserService userService;

    private Log log = LogFactory.getLog(LivingIdentityController.class);

    @PostMapping("/anon")
    @ResponseBody
    public ResultTo pushResult(HttpServletRequest request, String personalId, String name, Integer type, String userId, String url) {
        log.info("前端传入url" + url);
        String urlPath = ContextProperty.getContextProperty("urlPath.url") + request.getRequestURI();
        String personalIdEncrypt = enCryptAndEncode(personalId);
        String nameEncrypt = enCryptAndEncode(name);
        String userIdEncrypt = enCryptAndEncode(userId);
        String urlEncrypt = enCryptAndEncode(url);
        ApiResult apiResult = ApiUtil.sendGetSync(GetParam.newBuilder()
                        .add("personalId", personalId)
                        .add("name", name)
                        .add("api_key", PropertiesUtil.getProperty("verify.apiKey"))
                        .add("redirect_uri_suc", urlPath + "/success/" + personalIdEncrypt + "/" + nameEncrypt
                                + "/" + type + "/" + userIdEncrypt + "/" + urlEncrypt)
                        .add("redirect_uri_fail", urlPath + "/error/" + personalIdEncrypt + "/" + nameEncrypt
                                + "/" + type + "/" + userIdEncrypt + "/" + urlEncrypt)
                , ApiStore.LIVING_IDENTITY);
        JSONObject jsonObject = JSON.parseObject(apiResult.getData());
        return new ResultTo().setData(jsonObject.get("url"));
    }

    @GetMapping("/anon/success/{personalId}/{name}/{type}/{userId}/{url}")
    public ModelAndView success(@PathVariable String personalId, @PathVariable String name
            , @PathVariable Integer type, @PathVariable String userId, @PathVariable String url) {
        String urlPath = "";
        try {
            String identity = deCryptAndDecode(personalId);
            String userName = deCryptAndDecode(name);
            String id = deCryptAndDecode(userId);
            urlPath = deCryptAndDecode(url);
            switch (type) {
                case 0:
                    ZyCustomer customer = customerService.selectByPrimaryKey(id);
                    customer.setCardNum(identity);
                    customer.setCname(userName);
                    customer.setVerify(CustomerEnums.VerifyEnum.IS_VERIFY.getCode());
                    customerService.updateByPrimaryKeySelective(customer);
                    break;
                case 1:
                    CmuUser cmuUser = userService.selectByPrimaryKey(id);
                    cmuUser.setVerify(CustomerEnums.VerifyEnum.IS_VERIFY.getCode());
                    userService.updateByPrimaryKeySelective(cmuUser);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("解析后的url" + urlPath);
        ModelAndView modelAndView = new ModelAndView("views/success");
        modelAndView.addObject("url", urlPath);
        return modelAndView;
    }

    @GetMapping("/anon/error/{personalId}/{name}/{type}/{userId}/{url}")
    public ModelAndView error(@PathVariable String personalId, @PathVariable String name, @PathVariable Integer type, @PathVariable String userId, @PathVariable String url) {
        String urlPath = null;
        try {
            urlPath = deCryptAndDecode(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ModelAndView modelAndView = new ModelAndView("views/error");
        modelAndView.addObject("url", urlPath);
        return modelAndView;
    }

}
